<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
// API 模板文件，可以复制
namespace api\home\controller;
use think\Db;
use think\Validate;
use cmf\controller\RestUserBaseController;

class CompanyController extends RestUserBaseController
{

    /**
     * 根据项目ID查询人力资源情况表信息
     */
    public function human()
    {
        $data = $this->request->param();
        if (empty($data['pro_id'])) {
            $this->error("参数错误!");
        }

        $human = Db::name("pro_human")->where('pro_id', $data['pro_id'])->find();

        if (!empty($human)) {

            $human['number'] = json_decode($human['number']);
            $human['reason'] = unserialize($human['reason']);

            //查询附件
            $humanFile = Db::name("human_proof")->where('pro_human_id', $human['h_id'])->select()->toArray();

            if (!empty($humanFile)) {
                foreach ($humanFile as &$value) {
                    $value['time'] = date('Y-m-d H:i:s', $value['add_time']);
                    $value['url'] = $value['file'];
                    $value['filename'] = $value['file_name'];
                }
                unset($value);
            }

            $this->success('获取成功!', ['human' => $human, 'humanFile' => $humanFile]);
        } else {
            $this->success('暂无数据!', []);
        }
         
    }


    /**
     * 根据项目ID查询企业成长性情况表信息
     */
    public function growth()
    {
        $data = $this->request->param();
        if (empty($data['pro_id'])) {
            $this->error("参数错误!");
        }

        $growth = Db::name("pro_growth")->where('pro_id', $data['pro_id'])->find();

        if (!empty($growth)) {

            $growth['assets'] = json_decode($growth['assets']);

            //查询附件
            $growthFile = Db::name("growth_proof")
                                            ->alias('g')
                                            ->join('__PRO_ANNEX_TYPE__ t', 'g.file_type_id = t.t_id')
                                            ->field('g.*, t.type_name, t.t_id')
                                            ->where('pro_growth_id', $growth['g_id'])
                                            ->select()
                                            ->toArray();

            if (!empty($growthFile)) {
                foreach ($growthFile as &$val) {
                    $val['time'] = date('Y-m-d H:i:s', $val['add_time']);
                    $val['filename'] = $val['file_name'];
                    $val['url'] = $val['file'];
                }
                unset($val);
            }

            $this->success('获取成功!', ['growth' => $growth, 'growthFile' => $growthFile]);
        } else {
            $this->success('暂无数据!', []);
        }
        
    }



    /**
     * 根据项目ID查询附件清单表
     */
    public function annexList()
    {
        $data = $this->request->param();
        if (empty($data['pro_id'])) {
            $this->error("参数错误!");
        }

        $annex = Db::name("pro_annex")
                                ->alias('a')
                                ->join('__PRO_ANNEX_TYPE__ t', 'a.annex_type_id = t.t_id')
                                ->field('a.*, t.type_name, t.t_id')
                                ->where('pro_id', $data['pro_id'])
                                ->select()
                                ->toArray();

        if (!empty($annex)) {
            foreach ($annex as &$v) {
                $v['time'] = date('Y-m-d H:i:s', $v['a_annex_time']);
                $v['file_type_id'] = $v['annex_type_id'];
                $v['url'] = $v['a_annex_file'];
                $v['name'] = $v['a_annex_name'];
                $v['filename'] = $v['a_annex_filename'];
                $v['description'] = $v['annex_description'];
            }
            unset($v);
        }

        $this->success('获取成功!', $annex);
    }




    /**
     * 提交人力资源情况表信息
     */
    public function addHuman()
    {
        $role = $this->getUserRole();

        if ($role != '业务员' && $role != '技术员') {
            $this->error("你没有权限进行此操作!");
        }

        $data = $this->request->param();
        
        //判断有没有企业表ID
        if (empty($data['pro_id'])) {
            $this->error("参数错误!");
        }

        //人力资源情况表
        $pro_id = $data['pro_id'];
        $reason = isset($data['reason']) ? serialize($data['reason']) : '';

        //佐件附件表
        $file = isset($data['file']) ? $data['file'] : false;

        unset($data['pro_id'],$data['reason'],$data['file']);
               
        //添加人力资源情况表数据
        $humanId = Db::name("pro_human")->insertGetId([
            'pro_id'           =>  $pro_id,
            'number'           =>  json_encode($data),
            'reason'           =>  $reason
        ]);
            
        if (!empty($file)) {
            //添加佐件附件表
            foreach ($file as  $val) {
                Db::name("human_proof")->insert([
                'pro_human_id'     =>  $humanId,
                'file'             =>  substr(strrchr($val['url'], '/'), 1),
                'file_name'        =>  $val['filename'],
                'add_time'         =>  strtotime($val['time'])
                ]);
            }
            
        }

        if (empty($humanId)) {
            $this->error('提交失败，请重试!');
        }

        $this->success("提交成功!");
        


    }




    /**
     * 提交企业成长性情况
     *
     */
    public function addGrowth()
    {
        $role = $this->getUserRole();
        
        if ($role != '业务员' && $role != '技术员') {
            $this->error("你没有权限进行此操作!");
        }

        $data = $this->request->param();

        //判断有没有企业表ID
        if (empty($data['pro_id'])) {
            $this->error("参数错误!");
        }

        $pro_id = $data['pro_id'];

        //附件表
        $file = isset($data['file']) ? $data['file'] : false;

        unset($data['pro_id'], $data['file']);

        //添加企业成长性情况表数据
        $growthId =  Db::name("pro_growth")->insertGetId([
            'pro_id'           =>  $pro_id,
            'assets'           =>  json_encode($data)
        ]);

        if (!empty($file)) {
            //添加附件表
            foreach ($file as  $val) {
                Db::name("growth_proof")->insert([
                'pro_growth_id'     =>  $growthId,
                'file_type_id'     =>  $val['file_type_id'],
                'file'             =>  substr(strrchr($val['url'], '/'), 1),
                'file_name'        =>  $val['filename'],
                'add_time'         =>  strtotime($val['time'])
                ]);
            }
            
        }
            
        if (empty($growthId)) {
            $this->error('提交失败，请重试!');
        }

        $this->success("提交成功!");
    }



    /**
     * 删除企业成长性情况的附件
     * */
    public function delGrowthProof()
    {
        $role = $this->getUserRole();
        
        if ($role != '技术员' && $role != '业务员') {
            $this->error("你没有权限进行此操作!");
        }

        $data = $this->request->param();

        if (empty($data['id'])) {
            $this->error("参数错误!");
        }

        $result = Db::name("growth_proof")->where('pid', $data['id'])->delete();

        if (empty($result)) {
            $this->error("删除附件失败，请重试!");
        }

        $this->success("删除附件成功!");
    }


    
    /*** 
     * 附件清单添加附件提交
     * */
    public function addAnnexList()
    {
        $role = $this->getUserRole();
        
        if ($role != '业务员' && $role != '技术员') {
            $this->error("你没有权限进行此操作!");
        }

        $data = $this->request->param();

        if (empty($data['pro_id'])) $this->error("参数错误!");

        //附件表
        $file = isset($data['file']) ? $data['file'] : false;

        if(!empty($file)) {
            foreach ($file as  $val) {
                $result = Db::name("pro_annex")->insert([
                    'pro_id'             =>     $data['pro_id'],
                    'annex_type_id'      =>     $val['annex_type_id'],
                    'a_annex_file'       =>     substr(strrchr($val['url'], '/'), 1),
                    'a_annex_name'       =>     $val['name'],
                    'a_annex_filename'   =>     $val['filename'],
                    'a_annex_time'       =>     strtotime($val['time']),
                    'annex_description'  =>     $val['description']
                ]);

                if (empty($result)) {
                    $this->error("提交附件清单失败，请重试!");
                }
            }
        }

        $this->success("提交附件清单成功!");
    }



    /**
     * 附件清单的删除
     */
    public function delAnnex()
    {
        $role = $this->getUserRole();
        
        if ($role != '技术员' && $role != '业务员') {
            $this->error("你没有权限进行此操作!");
        }

        $data = $this->request->param();
        $ids = $data['id'];

        if (!$ids) {
            $this->error("参数错误!");
        }

        $result = Db::name("pro_annex")->where('pid', 'in', $ids)->delete();
        
        if (empty($result)) {
            $this->error("删除附件失败，请重试!");
        }

        $this->success("删除附件成功!");
    }




    /**
     * 编辑人力资源情况表
     */
    public function editHuman()
    {
        $role = $this->getUserRole();
        
        if ($role != '技术员' && $role != '业务员') {
            $this->error("你没有权限进行此操作!");
        }

        $data = $this->request->param();
        
        //判断有没有人力资源情况表ID
        if (empty($data['h_id'])) {
            $this->error("参数错误!");
        }

        //人力资源情况表
        $h_id = $data['h_id'];

        $reason = isset($data['reason']) ? serialize($data['reason']) : '';

        //佐件附件表
        $file = isset($data['file']) ? $data['file'] : false;

        unset($data['h_id'],$data['reason'],$data['file']);

        //添加人力资源情况表数据
        $human = Db::name("pro_human")->where('h_id', $h_id)->update([
            'number'           =>  json_encode($data),
            'reason'           =>  $reason
        ]);

        if (!empty($file)) {
            //查找文件进行删除
            Db::name("human_proof")->where('pro_human_id', $h_id)->delete();

            //添加佐件附件表
            foreach ($file as  $val) {
                Db::name("human_proof")->insert([
                'pro_human_id'     =>  $h_id,
                'file'             =>  $val['url'],
                'file_name'        =>  $val['filename'],
                'add_time'         =>  strtotime($val['time'])
                ]);
            }
            
        } else {
            //查找文件进行删除
            Db::name("human_proof")->where('pro_human_id', $h_id)->delete();
        }

        // if (empty($human)) {
        //     $this->error("修改失败，请重试!");
        // }

        $this->success("修改成功!");
        
            
    }



    /**
     * 编辑企业成长情况表
     */
    public function editGrowth()
    {
        $role = $this->getUserRole();
        
        if ($role != '技术员' && $role != '业务员') {
            $this->error("你没有权限进行此操作!");
        }

        $data = $this->request->param();

        //判断有没有企业表ID
        if (empty($data['g_id'])) {
            $this->error("参数错误!");
        }

        $g_id = $data['g_id'];

        //附件表
        $file = isset($data['file']) ? $data['file'] : false;

        unset($data['pro_id'], $data['file']);

        //添加企业成长性情况表数据
        $growth =  Db::name("pro_growth")->where('g_id', $g_id)->update(['assets'=>json_encode($data)]);

        if (!empty($file)) {
            //查找文件进行删除
            Db::name("growth_proof")->where('pro_growth_id', $g_id)->delete();

            //添加附件表
            foreach ($file as  $val) {
                Db::name("growth_proof")->insert([
                'pro_growth_id'     =>  $g_id,
                'file_type_id'     =>  $val['file_type_id'],
                'file'             =>  substr(strrchr($val['url'], '/'), 1),
                'file_name'        =>  $val['filename'],
                'add_time'         =>  strtotime($val['time'])
                ]);
            }
            
        } else {
            //查找文件进行删除
            Db::name("growth_proof")->where('pro_growth_id', $g_id)->delete();
        }
            
        // if (empty($growth)) {
        //     $this->error('修改失败，请重试!');
        // }

        $this->success("修改成功!");
    }



    /**
     * 编辑附件清单提交
     */
    public function editAnnex()
    {
        $role = $this->getUserRole();
        
        if ($role != '技术员' && $role != '业务员') {
            $this->error("你没有权限进行此操作!");
        }

        $data = $this->request->param();

        if (empty($data['pro_id'])) $this->error("参数错误!");

        //附件表
        $file = isset($data['file']) ? $data['file'] : false;

        if(!empty($file)) {

            //查找到属于该项目的附件清单数据进行删除
            Db::name("pro_annex")->where('pro_id', $data['pro_id'])->delete();

            //遍历进行添加
            foreach ($file as  $val) {
                $result = Db::name("pro_annex")->insert([
                    'pro_id'             =>     $data['pro_id'],
                    'annex_type_id'      =>     $val['annex_type_id'],
                    'a_annex_file'       =>     substr(strrchr($val['url'], '/'), 1),
                    'a_annex_name'       =>     $val['name'],
                    'a_annex_filename'   =>     $val['filename'],
                    'a_annex_time'       =>     strtotime($val['time']),
                    'annex_description'  =>     $val['description']
                ]);
            }
        } else {
            //查找到属于该项目的附件清单数据进行删除
            Db::name("pro_annex")->where('pro_id', $data['pro_id'])->delete();
        }

        $this->success("提交附件清单成功!");
    }



    
    /**
     * 人力资源情况表批注
     */
    public function HumanAnnotationPost()
    {
        $role = $this->getUserRole();
        
        if ($role != '审核员') {
            $this->error("你没有权限进行此操作!");
        }

        $data = $this->request->param();

        if (empty($data['id'])) $this->error("参数错误!");

        $id = $data['id'];
        unset($data['id']);

        $result = Db::name("pro_human")->where('h_id', $id)->update(['h_annotation' => json_encode($data)]);

        if (empty($result)) {
            $this->error("提交批注失败，请重试!");
        }

        $this->success("提交批注成功!");
    }




    /**
     * 企业成长性情况表批注
     */
    public function GrowthAnnotationPost()
    {
        $role = $this->getUserRole();
        
        if ($role != '审核员') {
            $this->error("你没有权限进行此操作!");
        }

        $data = $this->request->param();

        if (empty($data['id'])) $this->error("参数错误!");

        $id = $data['id'];
        unset($data['id']);

        $result = Db::name("pro_growth")->where('g_id', $id)->update(['g_annotation' => json_encode($data)]);

        if (empty($result)) {
            $this->error("提交批注失败，请重试!");
        }

        $this->success("提交批注成功!");
    }



    /**
     * 附件清单批注
     */
    public function annexAnnotationPost()
    {
        $role = $this->getUserRole();
        
        if ($role != '审核员') {
            $this->error("你没有权限进行此操作!");
        }

        $data = $this->request->param();

        if (empty($data['id'])) $this->error("参数错误!");

        $data['comment'] = isset($data['comment']) ? $data['comment'] : '';

        $result = Db::name("pro_annex_comment")->where('pro_id', $data['id'])->find();

        if ($result) {
            //存在就是修改
            $res = Db::name("pro_annex_comment")->where('pro_id', $data['id'])->update(['comment' => $data['comment']]);
        } else {
            //不存在就是添加
            $res = Db::name("pro_annex_comment")->insert([
                'pro_id' => $data['id'],
                'comment' => $data['comment']
            ]);
        }

        if (empty($res)) $this->error("提交批注失败，请重试!");

        $this->success("提交批注成功!");
    }


    /**
     * 查询附件清单的批注
     */
    public function getannexAnnotation()
    {
        $data = $this->request->param();

        if (empty($data['id'])) $this->error("参数错误!");

        $result = Db::name("pro_annex_comment")->where('pro_id', $data['id'])->find();

        $this->success('获取附件清单批注成功!', $result);

    }

}
